package com.example.tokendemo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.tokendemo.common.constant.Constants;
import com.example.tokendemo.common.domain.AjaxResult;
import com.example.tokendemo.core.entity.SysUserEntity;
import com.example.tokendemo.core.service.SysUserService;
import com.example.tokendemo.model.LoginBody;
import com.example.tokendemo.service.SysLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private SysUserService userService;

    @PostMapping("/login")
    public AjaxResult index(@RequestBody LoginBody loginBody){
        AjaxResult ajaxResult = AjaxResult.success();

        String token = loginService.login(loginBody.getUsername(),loginBody.getPassword(),loginBody.getCode(),loginBody.getUuid());

        ajaxResult.put(Constants.TOKEN, token);

        return ajaxResult;
    }


    @PostMapping("/register")
    @ResponseBody
    public AjaxResult register(@RequestBody JSONObject jsonObject){
        AjaxResult ajaxResult = AjaxResult.success();

        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        userService.addUser(new SysUserEntity(username, password));

        return ajaxResult;
    }
}
